﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIGameOver : MonoBehaviour
{
    public GameObject btnRestart, landSafely, yuhangyuan;
    public Text hight, maxhight, time, total, sunhao;
    public Slider sliderSunhao;
    // Start is called before the first frame update
    void Start()
    {
        UGUIEventListener.Get(btnRestart).onClick = (obj) =>
        {
            GameController.instance.rocket.GetComponent<rocket>().Restart();
            GameController.instance.ResetCamera();
            gameObject.SetActive(false);
        };
    }

    public void SetText(int _hight, int _time, int _goldtemp, float baifenbi, bool _issafe)
    {

        hight.text = "本次高度：" + _hight.ToString() + "，获得：$" + (_hight / 3).ToString();
        maxhight.text = "历史最高：" + PlayerPrefs.GetInt("maxhight").ToString() + (_hight > PlayerPrefs.GetInt("maxhight") ? "，新纪录+$200" : "");
        time.text = "飞行时间：" + _time.ToString() + "，获得：$" + _time.ToString();
        total.text = ((_hight / 3 + _time + _goldtemp) * (_issafe ? 2 : 1)).ToString();        
        int temp = PlayerPrefs.GetInt("gold", 200);
        PlayerPrefs.SetInt("gold", temp + ((_hight / 3 + _time + _goldtemp) * (_issafe ? 2 : 1)));

        if (baifenbi > 0.7f)
        {
            landSafely.SetActive(true);
            landSafely.transform.localScale = Vector3.one * 5;
            landSafely.transform.DOScale(1, 0.3f);
            for (int i = 0; i < 3; i++)
                yuhangyuan.transform.GetChild(i).Find("chacha").gameObject.SetActive(false);
            sliderSunhao.value = 1;
            sunhao.text = "perfect";
        }
        else
        {
            landSafely.SetActive(false);
            sliderSunhao.value = baifenbi;
            if (_issafe)
                sunhao.text = ((int)(baifenbi * 100)).ToString() + "%";
            else
                sunhao.text = "damaged";
            yuhangyuan.transform.GetChild(2).Find("chacha").gameObject.SetActive(true);
            if(baifenbi < 0.45f)
                yuhangyuan.transform.GetChild(1).Find("chacha").gameObject.SetActive(true);
            else
                yuhangyuan.transform.GetChild(1).Find("chacha").gameObject.SetActive(false);
            if (baifenbi < 0.2f)
                yuhangyuan.transform.GetChild(0).Find("chacha").gameObject.SetActive(true);
            else
                yuhangyuan.transform.GetChild(0).Find("chacha").gameObject.SetActive(false);
        }
    }
}